//
//  YSZWelcomeViewController.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/26.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

import SDWebImage

class YSZWelcomeViewController: UIViewController {

    //iconView的底部约束
    private var iconViewBottomCon: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //设置头像
        if let avatar_large = YSZUserAccount.loadUserAccount()?.avatar_large
        {
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        YSZUserAccount.loadUserAccount()?.loadUserInfo()
    }
    //准备ui
    private func prepareUI()
    {
        view.addSubview(backgroundImeageView)
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //添加约束
        
        //背景VFL
        backgroundImeageView.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImeageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImeageView]))
        
        //头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomCon!)
        //名称
        nameLabel.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    //添加头像动画
    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    //动画
    private func moveAnimation()
    {
        //当使用自动约束时， 不要修改view 的frame，bounds center，修改约束
        iconViewBottomCon?.constant = -UIScreen.mainScreen().bounds.height - iconViewBottomCon!.constant
        //动画
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //动画移动完成
                print("动画完成")
                AppDelegate.switchRootViewController(true)
        }
        
    }
    
    //懒加载图片
    private lazy var backgroundImeageView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    //用户头像
    private lazy var iconView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //圆角 宽高85
        imageView.layer.cornerRadius=42.5
        imageView.layer.masksToBounds=true
        return imageView
    }()
    //用户名称
    private lazy var nameLabel: UILabel =
    {
        let label = UILabel()
        label.text = "欢迎回来"
        //根据内容设置大小
        label.sizeToFit()
        return label
    }()
    

}
